package com.runvp.common.util;

import org.apache.commons.lang3.time.FastDateFormat;

/**
 * 订单相关号的创建
 *
 * @author xkk
 */
public class OrderNoUtils {

    private static ThreadLocal<StringBuilder> threadLocal = new ThreadLocal<StringBuilder>();
    private static final FastDateFormat pattern = FastDateFormat.getInstance("yyyyMMddHHmmssSSS");

    /**
     * 生成4 位验证码
     *
     * @return
     */
    public static String verifyCode() {
        return String.valueOf((int) ((Math.random() * 9 + 1) * 1000));
    }

    /**
     * 创建订单号 当前纳秒
     * <p>长度可变</p>
     *
     * @return str
     */
    public static String getOrderNo(String type) {
        return type+"_"+getOrderNo();
    }
    /**
     * 创建订单号 当前纳秒
     * <p>长度可变</p>
     *
     * @return str
     */
    public static String getOrderNo() {
        String body = String.valueOf(System.nanoTime());
        int length = body.length();
        StringBuilder sb = new StringBuilder(body);
        if (length > 14) {
            return body.substring(0,14);
        }
        int size = 14-length;
        for (int i = 0; i < size; i++) {
            sb.append(getOneRandomNum(9,1));
        }
        int l = sb.length();
        String lastNum = sb.substring(l-2,l);
        if("00".equals(lastNum)){
            return sb.substring(0,l-2)+getOneRandomNum(9,1)+getOneRandomNum(9,1);
        }
        return sb.toString();
    }

    /**
     * 获取范围内随机数
     * @param max
     * @param min
     * @return long
     */
    public static long getOneRandomNum(int max,int min){
        return (long) ((Math.random() * (max - min)) + min);
    }

    public static void main(String[] args) {
        String orn = getOrderNo()+"-"+getOrderNo();
        System.out.println(orn.substring(orn.length()-14,orn.length()));
    }
}
